package com.zz.springboot.initializer;

import com.zz.springboot.constant.TypeConstants;
import com.zz.springboot.manager.NumDictionaryManager;
import com.zz.springboot.model.NumDictionary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 号码字典表预加载管理类
 *
 * @desc 1.在Spring容器加载Bean完成后执行；2.一定要先创建好字典表，否则启动失败；要维护好数据库中号码字典表内容，否则预加载内容为空
 */
@Component
@ConditionalOnClass(NumDictionaryManager.class)
public class NumDictRunner implements CommandLineRunner {

    @Autowired
    private NumDictionaryManager numDictionaryManager;

    public static Map<String, List<Integer>> animal_Map = new HashMap();

    public static Map<String, List<Integer>> odds_evens_Map = new HashMap<>();

    public static Map<String, List<Integer>> bigLittle_Map = new HashMap<>();

    public static Map<String, List<Integer>> bigLittle_oddsEvens_combination_Map = new HashMap<>();

    public static Map<String, List<Integer>> color_Map = new HashMap<>();

    public static Map<String, List<Integer>> color_oddsEvens_combination_Map = new HashMap<>();

    public static Map<String, List<Integer>> tails_Map = new HashMap<>();

    public static Map<String, List<Integer>> head_Map = new HashMap<>();

    public static Map<String, List<Integer>> five_elements_Map = new HashMap<>();

    public static Map<String, List<Integer>> poultry_wild_Map = new HashMap<>();

    public static Map<Integer, NumDictionary> nums_dict_Map = new HashMap<>();


    @Override
    public void run(String... args) throws Exception {
        List<NumDictionary> numDictionaries = numDictionaryManager.selectAllData();
        //生肖维护
        for (String animal : TypeConstants.animalConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getAnimals().equals(animal)).map(u -> u.getNum()).collect(Collectors.toList());
            animal_Map.put(animal, numList);
        }
        //单双维护
        for (String odds_even : TypeConstants.oddEvenConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getOddsEvens().equals(odds_even)).map(u -> u.getNum()).collect(Collectors.toList());
            odds_evens_Map.put(odds_even, numList);
        }
        //大小维护
        for (String s : TypeConstants.bigLittleConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getBigLittle().equals(s)).map(u -> u.getNum()).collect(Collectors.toList());
            bigLittle_Map.put(s, numList);
        }
        //大小单双维护
        for (String bigLittle_oddsEvens : TypeConstants.bigLittleOddsEvensConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getBlOeCombination().equals(bigLittle_oddsEvens)).map(u -> u.getNum()).collect(Collectors.toList());
            bigLittle_oddsEvens_combination_Map.put(bigLittle_oddsEvens, numList);
        }
        //波色维护
        for (String color : TypeConstants.colorConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getColor().equals(color)).map(u -> u.getNum()).collect(Collectors.toList());
            color_Map.put(color, numList);
        }
        //波色单双维护
        for (String color_oddsEvens : TypeConstants.colorOddsEvensConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getColorOeCombination().equals(color_oddsEvens)).map(u -> u.getNum()).collect(Collectors.toList());
            color_oddsEvens_combination_Map.put(color_oddsEvens, numList);
        }
        //尾数维护
        for (String tail : TypeConstants.digitConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getTail().equals(tail)).map(u -> u.getNum()).collect(Collectors.toList());
            tails_Map.put(tail, numList);
        }
        //头数维护
        for (String head : TypeConstants.headConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getHead().equals(head)).map(u -> u.getNum()).collect(Collectors.toList());
            head_Map.put(head, numList);
        }
        //五行维护
        for (String five_element : TypeConstants.fiveElementsConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getFiveElements().equals(five_element)).map(u -> u.getNum()).collect(Collectors.toList());
            five_elements_Map.put(five_element, numList);
        }
        //家禽野兽维护
        for (String poultry_wild : TypeConstants.poultryWildConstant) {
            List<Integer> numList = numDictionaries.stream().filter(u -> u.getPoultryWild().equals(poultry_wild)).map(u -> u.getNum()).collect(Collectors.toList());
            poultry_wild_Map.put(poultry_wild, numList);
        }
        //号码映射关系总维护
        nums_dict_Map = numDictionaries.stream().collect(Collectors.toMap(u -> u.getNum(), u -> u));
    }

    public static Map<String, List<Integer>> getAnimal_Map() {
        return animal_Map;
    }

    public static Map<String, List<Integer>> getOdds_evens_Map() {
        return odds_evens_Map;
    }

    public static Map<String, List<Integer>> getBigLittle_Map() {
        return bigLittle_Map;
    }

    public static Map<String, List<Integer>> getBigLittle_oddsEvens_combination_Map() {
        return bigLittle_oddsEvens_combination_Map;
    }

    public static Map<String, List<Integer>> getColor_Map() {
        return color_Map;
    }

    public static Map<String, List<Integer>> getColor_oddsEvens_combination_Map() {
        return color_oddsEvens_combination_Map;
    }

    public static Map<String, List<Integer>> getTails_Map() {
        return tails_Map;
    }


    public static Map<String, List<Integer>> getHead_Map() {
        return head_Map;
    }

    public static Map<String, List<Integer>> getFive_elements_Map() {
        return five_elements_Map;
    }

    public static Map<String, List<Integer>> getPoultry_wild_Map() {
        return poultry_wild_Map;
    }

    public static Map<Integer, NumDictionary> getNumsDictMap() {
        return nums_dict_Map;
    }
}
